<?php
class PHZ_Log
{

    /**
     * 通过房间ID获得当前日志应该存储的表名称
     * 这里主要为了防止以后log表的日益增大；故采取使用房间号的后2位作为表后缀名称;不存在则创建
     * 数据库中；该类型表被分为了200个
     * @param $room_id  房间ID
     */
    public static function __gTn( $room_id ){
        return 'tt_room_log_copy' . ($room_id % 200);
    }

    /**
     * 前端用户轮询获取信息
     * @param $room_id    房间ID
     * @param $user_id    用户ID
     * @param $heartID    心跳值
     * @param $client_id  客户端ID
     * @param $db_connect 数据连接标识
     */
    public static function __gNlog( $room_id , $user_id , $heartID , $db_connect  ){
        if( $room_id == "" || $room_id == null ){
            return false;
        }
        $tableName    = self::__gTn( $room_id );
        $heartID      = (int)$heartID;
        $room_info    = PHZ_Memcache::__get_RoomInfo_ToMemcache( $room_id );
        if( $room_info == false ) {
            $room_info = PHZ_Room::__getRoomInfo_byID($room_id, $db_connect);
        }else{
            $room_info = json_decode( $room_info , true );
        }
        if( $room_info == false || $room_info == null || count( $room_info ) <= 0 ){
            return false;
        }
        $mem_log    = PHZ_Memcache::__getgNlog_DATA(  $room_info['room_id'] , $room_info['room_js'] , $user_id );
        if( $mem_log != false ){
            $mem_data = json_decode( $mem_log , true );
            foreach ( $mem_data as $key => $value ){
                if( $value['id'] > $heartID ){
                    $value['log_data'] = json_decode( $value['log_data'] , true );
                    $value['log_f']    = "mem";
                    return $value;break;
                }
            }
        }else {
            $new_log_info = $db_connect->query("select * from " . $tableName . " where `log_jid` = " . $room_info['room_js'] . " and  `id` > " . $heartID . " and log_to_uid = '" . $user_id . "' and log_rid = '" . $room_id . "' order by id asc limit 1");
            if (count($new_log_info) >= 1) {
                $new_log_info = $new_log_info[0];
                if ($new_log_info['log_to_uid'] == $user_id) {
                    $new_log_info['log_data'] = json_decode($new_log_info['log_data'], true);
                    $new_log_info['log_f']    = "ms";
                    return $new_log_info;
                }
            }
        }
        return false;
    }

    /**
     * 发送用户日志(所有人)（这个待强化）
     * @param $room_info
     * @param $send_uid
     * @param $temp
     * @param $db_connect
     * @return bool
     */
    public static function __send_user_log( $room_info , $send_uid ,  $temp , $db_connect , $log_md = 15 ){
        foreach( $room_info as $key => $value ) {
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                $data = array();
                $data['log_rid']     = $room_info['room_id'];
                $data['log_jid']     = $room_info['room_js'];
                $data['log_uid']     = $send_uid;//消息执行发送者的ID
                $data['log_to_uid']  = $value;
                $data['log_md']      = $log_md;
                $data['log_data']    = json_encode( $temp );//数据包
                $db_connect->insert( self::__gTn( $room_info['room_id'] ) )->cols( $data )->query();
                PHZ_Card::__update_MAX_HeaderID( $room_info , $value , $db_connect );//更新用户的当前最大可执行ID
            }
        }
        return true;
    }

    public static function __send_user_log_IN_Single( $room_info , $send_uid , $send_to_uid , $temp ,  $db_connect ){
        $data = array();
        $data['log_rid']     = $room_info['room_id'];
        $data['log_jid']     = $room_info['room_js'];
        $data['log_uid']     = $send_uid;//消息执行发送者的ID
        $data['log_to_uid']  = $send_to_uid;
        $data['log_md']      = 15;
        $data['log_data']    = json_encode( $temp );//数据包
        $db_connect->insert( self::__gTn( $room_info['room_id'] ) )->cols( $data )->query();
        PHZ_Card::__update_MAX_HeaderID( $room_info , $send_to_uid , $db_connect );//更新用户的当前最大可执行ID
        return true;
    }

    /**
     * 获取一个房间中的所有游戏记录（单局）
     * //优化：这里只读取md为15的信息，所以切记，只要是跟游戏有关的操作；都要记录为15
     * @param $room_id
     * @param $room_js
     * @param $db_connect
     */
    public static function __getRoomJS_ALLDATA( $room_id , $room_js , $user_id , $db_connect ) {
        $where = "log_md = 15 AND log_to_uid = ". $user_id ." and log_rid = " . $room_id . " AND log_jid = " . $room_js;
        return $db_connect->select('*')->from( self::__gTn( $room_id ) )->where( $where )->query();
    }

    /**
     * 删除一个房间的所有日志信息
     * @param $room_id
     * @param $db_connect
     */
    public static function __DeleteRoom_ALLLOG( $room_id , $db_connect ){
        $where = "log_rid = " . $room_id;
        $db_connect->delete( self::__gTn( $room_id ) )->where( $where )->query();
        return true;
    }

    /**
     * 如果有用户选择了退出，那么将清除他的入房日志
     * @info::
     * @param $room_id
     * @param $room_js
     * @param $user_id
     * @param $db_connect
     */
    public static function __Delete_JieSan_USERLOG( $room_id , $room_js , $user_id , $db_connect ){
        //清除退出者的日志
        $where = "log_md = 14 AND (log_uid = " . $user_id . " OR log_to_uid = ". $user_id .") AND log_rid = " . $room_id . " AND log_jid = " . $room_js;
        $db_connect->delete( self::__gTn( $room_id ) )->where( $where )->query();
        //更新其他人的日志；将log_data中剔除这个退出的人
        $where = "log_md = 14 AND log_rid = " . $room_id . " AND log_jid = " . $room_js;
        $log_info = $db_connect->select('*')->from( self::__gTn( $room_id ) )->where( $where )->query();
        foreach ( $log_info as $key => $value ){
            $log_data = json_decode( $value['log_data'] , true );
            foreach ( $log_data as $key_1 => $value_1 ){
                if( $value_1 == $user_id ){
                    $outUserINDEX_N = substr($key_1,0,1);//取出第一个字母
                    $data = $log_data;
                    $data[$outUserINDEX_N . 'u'] = 0;
                    $data[$outUserINDEX_N . 'n'] = "";
                    $data[$outUserINDEX_N . 'f'] = "";
                    $db_connect->update( self::__gTn( $room_id ) )->cols(array('log_data'=> json_encode( $data ) ))->where('id=' . $value['id'] )->query();
                    break;
                }
            }
        }
        return true;
    }

}






